import axios from 'axios'
import vuex_store from './vuex-store'
import qs from 'qs'
import Vue from 'vue'
axios.defaults.timeout = 5000;                        //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';           //配置请求头
axios.defaults.baseURL = '//api.leson-medical.com/api/h5api/';   //配置接口地址
// axios.defaults.baseURL = '//192.168.0.128:8000/api/h5api/';   //配置接口地址

//在发送请求之前做某件事
axios.interceptors.request.use((config) => {
    //POST传参序列化(添加请求拦截器)
    if(config.method  === 'post'){
        config.data = qs.stringify(config.data);
    }
    //配置token
    if (vuex_store.getters.getUserToken) {

       config.headers.Authorization = 'Bearer ' + vuex_store.getters.getUserToken;

     }
     //配置操作码code
      if (vuex_store.getters.getAuth) {
        // console.log("codetoken:" + vuex_store.getters.getAuth)
       config.headers.common["codetoken"]   = '' + vuex_store.getters.getAuth;

     }


    return config;
},(error) =>{


    alert("错误的传参")

    return Promise.reject(error);
});
//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) =>{
  //对响应数据做些事
    if(!res.data.success){
        // _.toast(res.data.msg);
        return Promise.resolve(res);
    }
    return res;
}, (error) => {

    if(error.response.status == 401){

      console.log("401:没有手机号码")
      vuex_store.commit("userToken","");
      window.location.href= '/#/Login'

      Store.save("/Login",Store.CURRENTROUTE_KEY);
     
    }

    if(error.response.status == 402){

      console.log("402:没有操作或者过期了")
      vuex_store.commit("setAuth","");
      window.localStorage.setItem('auth',"");


    }
    return Promise.reject(error);

});
// 返回一个Promise(默认发送post请求)
export function fetch(url, params,method) {

    return new Promise((resolve, reject) => {

        method=method||'post';

       axios({
            method:method,
            url:url,
            data:params
        }).then(response => {
            resolve(response);
          }, err => {

              reject(err);
          })
          .catch((error) => {
            console.log("catch")
             reject(error)
          });
   })
}

export default {
    /**
     * 用户登录
     */
    getToken(params,method) {
        return fetch('signin1', params,method)
    } ,
    getAuth(params){

      return fetch('signin2',params)
    },
    getBaseMessage(params){

      return fetch('baseinfo',params,"get")
    },
    editBaseMessage(params){

      return fetch('baseinfo',params,"patch")
    },
    getIVF(){
      return fetch('ivf',{},'get')
    },
    setIVF(params){
      // : date 、 content 、 plan
      return fetch('ivf',params)
    },
    setOvulationBase(params){
      return fetch('ovulation/baseinfo',params,'patch')
    },
    setOvulationDetail(params,method){
      return fetch('ovulation',params,method)
    },
    getOvulation(){
      return fetch('ovulation',{},'get')
    },
    //身体检查PhysicalExamination
    getPhysicalExamination(params){
      var url = "bodyreport/classid/"+params;
      return fetch(url,{},"get")
    },
    getOvulation(){
      return fetch("ovulation",{},'get')
    },
    getFreezingStorage(){
      return fetch("frozen",{},"get")
    },
    setFreezingStorage(params){
      return fetch("frozen",params,"post")
    },
    editFreezingStorage(params){
      return fetch("frozen",params,"patch")
    },
    getEmbrosMove(){
      return fetch("embryo",{},"get")
    },
    setEmbrosMove(params){
      return fetch("embryo",params,"post")
    },
    editEmbrosMove(params){
      return fetch("embryo",params,"patch")
    },
    getEmbryosInduction(){
      return fetch('fertilization',{},"get")
    },
    editEmbryosInduction(params){
      return fetch('fertilization',params,"patch")
    },
    getIngravidation(){
      return fetch('pregnancy',{},"get")
    },
    editIngravidation(params){
      return fetch('pregnancy',params,"patch")
    }




}
